﻿using Grpc.Core;
using GrpcServer.Web.Date;
using GrpcServer.Web.protos;
namespace GrpcServer.Web.Servives
{
    public class MyEmployeeService : EmployeeService.EmployeeServiceBase
    {
        private ILogger<MyEmployeeService> logger;
        private List<Employee> employees;
        public MyEmployeeService(ILogger<MyEmployeeService> logger) 
        {
            this.logger = logger;
            employees = InMemoryData.Employees();
        }

        public override async  Task<EmployeeResponse> GetByNo(GetByNoRequest request, ServerCallContext context)
        {
            var md = context.RequestHeaders;
            foreach (var pair in md)
            {
                logger.LogInformation($"{pair.Key}:{pair.Value}");
            }
            var employee = employees.SingleOrDefault(x => x.No == request.No);
            if(employee != null)
            {
                var response = new EmployeeResponse
                {
                    Employee = employee,
                };
                return response;
            }
            throw new Exception($"Employee not found with no:{request.No}");
        }

        public override async Task GetAll(GetAllRequest request, IServerStreamWriter<EmployeeResponse> responseStream, ServerCallContext context)
        {
            foreach(var employee in employees) 
            {
                await responseStream.WriteAsync(new EmployeeResponse
                {
                    Employee = employee
                });
            }
        }

        public override async Task<AddPhotoResponse> AddPhoto(IAsyncStreamReader<AppPhotoRequest> requestStream, ServerCallContext context)
        {
            Metadata md = context.RequestHeaders;
            foreach (var pair  in md)
            {
                logger.LogInformation($"{pair.Key}:{pair.Value}");
            }

            var data = new List<byte>();
            while (await requestStream.MoveNext())
            {
                logger.LogInformation($"Received:{requestStream.Current.Data.Length} bytes");
                data.AddRange(requestStream.Current.Data);
            }
            logger.LogInformation($"Received file with {data.Count} bytes");
            return new AddPhotoResponse { IsOk = true};
        }

        public override async Task SaveAll(IAsyncStreamReader<EmployeeRequest> requestStream, IServerStreamWriter<EmployeeResponse> responseStream, ServerCallContext context)
        {
            //接收请求
            while(await requestStream.MoveNext())
            {
                var emplayee = requestStream.Current.Employee;
                employees.Add(emplayee);
                await responseStream.WriteAsync(new EmployeeResponse()
                {
                    Employee = emplayee
                });
            }
            logger.LogInformation("Employees:");
            foreach (var employee in employees)
            {
                Console.WriteLine(employee);
            }
        }
    }
}
